package com.ezjie.ielts.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.ezjie.ielts.model.CadicatesBean;
import com.ezjie.ielts.model.EwordInstance;
import com.ezjie.ielts.model.EwordMeaning;
import com.ezjie.ielts.model.EwordQuestion;
import com.ezjie.ielts.model.Instance;
import com.ezjie.ielts.model.Mean;
import com.ezjie.ielts.model.Question;

public class ListUtils {

	public static int indexOf(String questionId, List<Map<String, Object>> list) {
		int index = -1;
		if (TextUtils.isEmpty(questionId) || list == null || list.size() == 0) {
			index = -1;
		}
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = list.get(i);
			String id = (String) map.get("question_id");
			if (questionId.equals(id)) {
				index = i;
				break;
			}
		}
		return index;
	}

	/**
	 * 获得听力的类型
	 * 
	 * @param classifyId
	 * @param listeningConversation
	 * @param listeningLecture
	 * @return
	 */
	public static String getListeningType(String classifyId, List<Map<String, Object>> listeningConversation,
			List<Map<String, Object>> listeningLecture) {
		String type = "Listening";
		if (listeningConversation != null && listeningConversation.size() > 0) {
			for (int i = 0; i < listeningConversation.size(); i++) {
				Map<String, Object> map = listeningConversation.get(i);
				String id = (String) map.get("id");
				if (id.equals(classifyId)) {
					type = "Conversation";
					break;
				}
			}
		}
		if (listeningLecture != null && listeningLecture.size() > 0) {
			for (int i = 0; i < listeningLecture.size(); i++) {
				Map<String, Object> map = listeningLecture.get(i);
				String id = (String) map.get("id");
				if (id.equals(classifyId)) {
					type = "Lecture";
					break;
				}
			}
		}

		return type;
	}

	// 获得正确答案集合 按照answer_order顺序
	public static ArrayList<Integer> getRightList(ArrayList<Integer> correctList, List<Map<String, Object>> answerList) {
		if (correctList == null || answerList == null) {
			return null;
		}
		try {
			ArrayList<Map<String, Object>> rightList = new ArrayList<Map<String, Object>>();

			// 首先获得正确答案的集合list1
			for (Integer index : correctList) {
				rightList.add(answerList.get(index));
			}
			ArrayList<Map<String, Object>> list1 = deepcopy(rightList);

			// 按照answer_order获得正确顺序的 正确答案list2
			List<Map<String, Object>> list2 = rightList;
			Collections.sort(list2, new Comparator<Map<String, Object>>() {

				@Override
				public int compare(Map<String, Object> lhs, Map<String, Object> rhs) {
					// TODO Auto-generated method stub
					double first = (Double) lhs.get("answer_order");
					double second = (Double) rhs.get("answer_order");
					return (int) (first - second);
				}
			});
			// 根据正确顺序，获得 正确的下标顺序
			ArrayList<Integer> indexs = new ArrayList<Integer>();
			for (Map<String, Object> map : list2) {
				int indexOf = list1.indexOf(map);
				indexs.add(indexOf);
			}
			return indexs;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 对list的深度复制
	 * 
	 * @param src
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static ArrayList deepcopy(ArrayList src) throws IOException, ClassNotFoundException {

		ByteArrayOutputStream byteout = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(byteout);
		out.writeObject(src);
		ByteArrayInputStream bytein = new ByteArrayInputStream(byteout.toByteArray());
		ObjectInputStream in = new ObjectInputStream(bytein);
		ArrayList dest = (ArrayList) in.readObject();

		return dest;

	}

	/**
	 * 将 按 0 ， 1， 2 排序的list转化成 a，b，c
	 * 
	 * @param sortList
	 * @return
	 */
	public static String sortStrByList(List<Integer> sortList) {
		StringBuffer sBuffer = new StringBuffer();
		sBuffer.append("[");
		for (int i = 0; i < sortList.size(); i++) {
			if (i == sortList.size() - 1) {
				sBuffer.append(abcd(sortList.get(i)));
			} else {
				sBuffer.append(abcd(sortList.get(i)) + ",");
			}
		}
		sBuffer.append("]");
		return sBuffer.toString();
	}

	public static String abcd(Integer integer) {
		String string = "";
		switch (integer) {
		case 0:
			string = "A";
			break;
		case 1:
			string = "B";
			break;
		case 2:
			string = "C";
			break;
		case 3:
			string = "D";
			break;
		case 4:
			string = "E";
			break;
		case 5:
			string = "F";
			break;
		default:
			break;
		}
		return string;
	}

	/**
	 * 将list转化为 逗号 分割的字符串
	 * 
	 * @return
	 */
	public static String getListStrByDouhao(List<Integer> list) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			Integer integer = list.get(i);
			if (i == list.size() - 1) {
				sBuffer.append(integer + "");
			} else {
				sBuffer.append(integer + ",");
			}
		}
		return sBuffer.toString();
	}

	/**
	 * 
	 * @param list
	 *            认识单词的下标位置
	 * @param allList
	 *            所有的单词
	 * @return
	 */
	public static String getWidStrByDouhao(List<Integer> list, List<CadicatesBean> allList) {
		StringBuffer sBuffer = new StringBuffer();
		try {
			List<CadicatesBean> list2 = new ArrayList<CadicatesBean>();
			for (int i = 0; i < list.size(); i++) {
				list2.add(allList.get(list.get(i)));
			}
			for (int i = 0; i < list2.size(); i++) {
				CadicatesBean cadicatesBean = list2.get(i);
				if (i == list2.size() - 1) {
					sBuffer.append(cadicatesBean.getWid() + "");
				} else {
					sBuffer.append(cadicatesBean.getWid() + ",");
				}
			}
		} catch (Exception e) {
			LogUtils.d("列表转换异常");
			sBuffer.append("");
		}

		return sBuffer.toString();
	}

	public static String getWidStrByDouhao(List<CadicatesBean> list) {
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			CadicatesBean cadicatesBean = list.get(i);
			if (i == list.size() - 1) {
				sBuffer.append(cadicatesBean.getWid() + "");
			} else {
				sBuffer.append(cadicatesBean.getWid() + ",");
			}
		}
		return sBuffer.toString();
	}

	public static String getTransferStrByList(List<Mean> means) {
		StringBuffer sBuffer = new StringBuffer();
		if (means != null && means.size() > 0) {
			for (int i = 0; i < means.size(); i++) {
				Mean mean = means.get(i);
				if (i == means.size() - 1) {
					sBuffer.append(mean.getPos() + mean.getText());
				} else {
					sBuffer.append(mean.getPos() + mean.getText()).append("<br>");
				}
			}
		}

		return sBuffer.toString();
	}

	public static String getTransferStr1ByList(List<Mean> means) {
		StringBuffer sBuffer = new StringBuffer();
		if (means != null && means.size() > 0) {
			for (int i = 0; i < 1; i++) {
				Mean mean = means.get(i);

				sBuffer.append(mean.getPos() + mean.getText());

			}
		}

		return sBuffer.toString();
	}

	public static String getExampleStrByList(List<Instance> instances) {
		StringBuffer sBuffer = new StringBuffer();
		if (instances != null && instances.size() > 0) {
			for (int i = 0; i < instances.size(); i++) {
				Instance instance = instances.get(i);
				if (i == instances.size() - 1) {
					sBuffer.append(instance.getOrigin());
				} else {
					sBuffer.append(instance.getOrigin()).append("\n");
				}
			}
		}

		return sBuffer.toString();
	}

	/**
	 * 只显示第一个 例句
	 * 
	 * @param instances
	 * @return
	 */
	public static String getExampleStrFor1ByList(List<Instance> instances) {
		StringBuffer sBuffer = new StringBuffer();
		if (instances != null && instances.size() > 0) {
			Instance instance = instances.get(0);
			sBuffer.append(instance.getOrigin());
		}

		return sBuffer.toString();
	}

	public static List<Mean> transferMeanList(List<EwordMeaning> list) {
		List<Mean> means = new ArrayList<Mean>();
		if (list != null && list.size() > 0) {
			for (EwordMeaning ewordMeaning : list) {
				Mean mean = new Mean();
				mean.setPos(ewordMeaning.getPos());
				mean.setText(ewordMeaning.getText());
				means.add(mean);
			}
		}
		return means;
	}

	public static List<Instance> transferInstanceList(List<EwordInstance> list) {
		List<Instance> instances = new ArrayList<Instance>();
		if (list != null && list.size() > 0) {
			for (EwordInstance ewordInstance : list) {
				Instance instance = new Instance();
				instance.setOrigin(ewordInstance.getOrigin());
				instance.setTranslation(ewordInstance.getTranslation());
				instances.add(instance);
			}
		}
		return instances;
	}

	public static List<Question> transferQuestionList(List<EwordQuestion> list) {
		List<Question> questions = new ArrayList<Question>();
		if (list != null && list.size() > 0) {
			for (EwordQuestion ewordQuestion : list) {
				if (1 == ewordQuestion.getQuestion_type()) {
					String questionStr = ewordQuestion.getQuestions();
					if (!TextUtils.isEmpty(questionStr)) {
						questions = JSON.parseArray(questionStr, Question.class);
					}
				}
			}
		}
		return questions;
	}

	public static int transferQuestionType(List<EwordQuestion> list) {
		int questionType = 0;
		if (list != null && list.size() > 0) {
			for (EwordQuestion ewordQuestion : list) {
				questionType = ewordQuestion.getQuestion_type();
			}
		}
		return questionType;
	}

	/**
	 * 过滤出选择题
	 * 
	 * @param wordQuestions
	 * @return
	 */
	public static List<EwordQuestion> filterChooseQuestion(List<EwordQuestion> wordQuestions) {
		List<EwordQuestion> list = new ArrayList<EwordQuestion>();
		if (wordQuestions != null) {
			for (EwordQuestion ewordQuestion : wordQuestions) {
				if (ewordQuestion.getQuestion_type() == 1) {
					list.add(ewordQuestion);
				}
			}
		}
		return list;
	}
}
